package message.service;

import message.base.HandlerManager;
import message.entity.MsgConfig;
import message.handler.IMsgHandler;
import org.dom4j.DocumentException;

import java.util.Map;

public class MsgService {

    private static final HandlerManager HANDLER_MANAGER = HandlerManager.getDefault();

    public MsgConfig getMsgConfig(String path) throws DocumentException {
        // TODO: 获取msgconfig,可以对msgConfig进行缓存，直接从缓存中获取，减少重复解析
        return new MsgConfig(path);
    }

    public byte[] pack (MsgConfig msgConfig, Map<String, Object> source) throws  Exception{
        // 按报文类型获取
        IMsgHandler handler = HANDLER_MANAGER.getHandler(msgConfig.getMsgType());
        byte[] result = handler.pack(msgConfig, source);
        return result;
    }

    public Map<String, Object> unPack (MsgConfig msgConfig, byte[] rspData) throws  Exception{
        // 按报文类型获取
        IMsgHandler handler = HANDLER_MANAGER.getHandler(msgConfig.getMsgType());
        Map<String, Object> map = handler.unPack(msgConfig, rspData);
        return map;
    }

}
